home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / adg_4_6.zip / PRINT.C < prev    next >
C/C++ Source or Header  |  1991-02-21  |  6KB  |  192 lines

  1. /****************************************************************************
  2. Module name: Print.C
  3. Programmer : Jeffrey M. Richter.
  4. *****************************************************************************/
  5.  
  6. #include "..\nowindws.h"
  7. #undef NOCOLOR
  8. #undef NOCTLMGR
  9. #undef NOGDI
  10. #undef NOKERNEL
  11. #undef NOLSTRING
  12. #undef NOMB
  13. #undef NOMEMMGR
  14. #undef NOMENUS
  15. #undef NOMSG
  16. #undef NOSHOWWINDOW
  17. #undef NOUSER
  18. #undef NOWINMESSAGES
  19. #undef NOWINSTYLES
  20. #include <windows.h>
  21. #include <drivinit.h>
  22.  
  23. #include "print.h"
  24. #include "printstp.h"
  25.  
  26. char _szAppName[] = "Print";
  27. char _szText[] = "This is a text line!";
  28. HANDLE _hInstance = NULL;
  29.  
  30. LONG FAR PASCAL WndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam);
  31. BOOL FAR PASCAL AboutProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam);
  32.  
  33. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  34.                     LPSTR lpszCmdLine, int nCmdShow) {
  35.    HWND hWnd; MSG msg; WNDCLASS wc;
  36.  
  37.    _hInstance = hInstance;
  38.    if (!hPrevInstance) {
  39.       wc.style         = 0;
  40.       wc.lpfnWndProc   = WndProc;
  41.       wc.cbClsExtra    = 0;
  42.       wc.cbWndExtra    = 0;
  43.       wc.hInstance     = hInstance;
  44.       wc.hIcon         = LoadIcon(hInstance, _szAppName);
  45.       wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  46.       wc.hbrBackground = COLOR_WINDOW + 1;
  47.       wc.lpszMenuName  = _szAppName;
  48.       wc.lpszClassName = _szAppName;
  49.       if (!RegisterClass(&wc)) return(0);
  50.    }
  51.  
  52.    hWnd = CreateWindow(_szAppName, _szAppName,
  53.       WS_OVERLAPPED | WS_VISIBLE | WS_CAPTION |
  54.       WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME, 
  55.       CW_USEDEFAULT, nCmdShow, CW_USEDEFAULT, CW_USEDEFAULT,
  56.       NULL, NULL, hInstance, 0);
  57.    if (hWnd == NULL) return(0);
  58.  
  59.    while (GetMessage(&msg, NULL, 0, 0)) {
  60.       TranslateMessage(&msg);
  61.       DispatchMessage(&msg);
  62.    }
  63.  
  64.    return(0);
  65. }
  66.  
  67.  
  68.  
  69. GLOBALHANDLE _hMemPS1 = NULL, _hMemPS2 = NULL;
  70.  
  71. LONG FAR PASCAL WndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam) {
  72.    BOOL fCallDefProc = FALSE;
  73.    LONG lResult = 0;
  74.    GLOBALHANDLE hMemNewPS;
  75.    HDC hDC;
  76.    LPPSSTRUCT lpPS;
  77.    FARPROC fpProc;
  78.    char szNullPort[MAXPORTLEN];
  79.  
  80.    switch (wMsg) {
  81.       case WM_CREATE:
  82.          _hMemPS1 = SetupDefPrinter();
  83.          if (_hMemPS1 == NULL)
  84.             EnableMenuItem(GetMenu(hWnd), IDM_PRINT1, MF_GRAYED | MF_BYCOMMAND);
  85.  
  86.          _hMemPS2 = SetupDefPrinter();
  87.          if (_hMemPS2 == NULL)
  88.             EnableMenuItem(GetMenu(hWnd), IDM_PRINT2, MF_GRAYED | MF_BYCOMMAND);
  89.          break;
  90.  
  91.       case WM_DESTROY:
  92.          GlobalFree(_hMemPS1);
  93.          GlobalFree(_hMemPS2);
  94.          PostQuitMessage(0);
  95.          break;
  96.  
  97.       case WM_COMMAND:
  98.          switch (wParam) {
  99.  
  100.             case IDM_ABOUT:
  101.                fpProc = MakeProcInstance(AboutProc, _hInstance);
  102.                DialogBox(_hInstance, "About", hWnd, fpProc);
  103.                FreeProcInstance(fpProc);
  104.                break;
  105.  
  106.             case IDM_PS1:
  107.             case IDM_PS2:
  108.                if (wParam == IDM_PS1) {
  109.                   hMemNewPS = PrinterSetup(hWnd, _hMemPS1, PSMODE_ACTIVEONLY);
  110.                   if (hMemNewPS == NULL) break;
  111.                   GlobalFree(_hMemPS1);
  112.                   _hMemPS1 = hMemNewPS;
  113.                } else {
  114.                   hMemNewPS = PrinterSetup(hWnd, _hMemPS2, 0);
  115.                   if (hMemNewPS == NULL) break;
  116.                   GlobalFree(_hMemPS2);
  117.                   _hMemPS2 = hMemNewPS;
  118.                }
  119.  
  120.                lpPS = (LPPSSTRUCT) GlobalLock(hMemNewPS);
  121.                GetProfileString("windows", "NullPort", "",
  122.                   szNullPort, sizeof(szNullPort));
  123.  
  124.                // Gray menu item if print is on NullPort.
  125.                EnableMenuItem(GetMenu(hWnd),
  126.                   (wParam == IDM_PS1) ? IDM_PRINT1 : IDM_PRINT2,
  127.                   MF_BYCOMMAND | (lstrcmpi(szNullPort, lpPS->szPort) ?
  128.                   MF_ENABLED : MF_GRAYED));
  129.                GlobalUnlock(hMemNewPS);
  130.                break;
  131.  
  132.             case IDM_PRINT1:
  133.             case IDM_PRINT2:
  134.                hDC = CreatePrinterDC(
  135.                   (wParam == IDM_PRINT1) ? _hMemPS1 : _hMemPS2);
  136.  
  137.                if (hDC == NULL) {
  138.                   MessageBox(hWnd, "Cannot print.", _szAppName,
  139.                      MB_OK | MB_ICONEXCLAMATION);
  140.                   break;
  141.                }
  142.                Escape(hDC, STARTDOC, lstrlen(_szAppName), _szAppName, 0);
  143.                TextOut(hDC, 10, 10, _szText, lstrlen(_szText));
  144.                Escape(hDC, NEWFRAME, 0, 0, 0);
  145.                Escape(hDC, ENDDOC, 0, 0, 0);
  146.                DeleteDC(hDC);
  147.                break;
  148.          }
  149.          break;
  150.  
  151.       default:
  152.          fCallDefProc = TRUE;
  153.          break;
  154.    }
  155.    if (fCallDefProc)
  156.       lResult = DefWindowProc(hWnd, wMsg, wParam, lParam);
  157.    return(lResult);
  158. }
  159.  
  160. // ***************************************************************************
  161. // This function processes all messages sent to the About dialog box.
  162.  
  163. BOOL FAR PASCAL AboutProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam) {
  164.    char szBuffer[100];
  165.    BOOL fProcessed = TRUE;
  166.  
  167.    switch (wMsg) {
  168.  
  169.       case WM_INITDIALOG:
  170.          // Set version static window to have date and time of compilation.
  171.          wsprintf(szBuffer, "%s at %s", (LPSTR) __DATE__, (LPSTR) __TIME__);
  172.          SetWindowText(GetDlgItem(hDlg, ID_VERSION), szBuffer);
  173.          break;
  174.  
  175.       case WM_COMMAND:
  176.          switch (wParam) {
  177.             case IDOK: case IDCANCEL:
  178.                if (HIWORD(lParam) == BN_CLICKED)
  179.                   EndDialog(hDlg, wParam);
  180.                break;
  181.  
  182.             default:
  183.                break;
  184.          }
  185.          break;
  186.  
  187.       default:
  188.          fProcessed = FALSE; break;
  189.    }
  190.    return(fProcessed);
  191. }
  192.